﻿using UnityEngine;
using System.Collections;

public class MainCharacterScript : MonoBehaviour {

	public GameObject AnimationsCharacter;
	public GameObject paraticle;

	private Animator anima;

	private Vector3 velocity;					// van toc di chuyen
	private Vector2 JumpFore;					// luc nhay
	private Vector2	MovingFore;					//luc di chuyen

	private bool airflag=true;

	// Use this for initialization
	void Start () {
		anima = AnimationsCharacter.GetComponent<Animator> ();
		velocity = new Vector3 (3,0,0);
		JumpFore = new Vector2 (0,200);
		MovingFore = new Vector2 (10,0);
	}
	
	// Update is called once per frame
	/*void FixUpdate()
	{
		rigidbody2D.AddForce (MovingFore);
		if(Input.GetKey(KeyCode.Space)){
			rigidbody2D.AddForce(JumpFore);
		}
	}
*/
	void Update()
	{
		Debug.Log (airflag);
		/*if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.RightArrow)) {
						Debug.Log ("Running....");
						anima.SetBool ("running", true);
			anima.SetBool("Jump",false);
				} else {
			anima.SetBool("running",false);
			anima.SetBool ("Jump", true);
		}
		*/
		gameObject.transform.Translate (velocity*Time.deltaTime);

		if (Input.GetKey (KeyCode.Space)&&airflag==true) {
						anima.SetBool ("running", false);
						anima.SetBool ("Jump", true);
						rigidbody2D.AddForce(JumpFore);
						airflag =false;													// ko dang nhay
				} else {
			//MainCharacter.transform.Translate (velocity*Time.deltaTime);
			anima.SetBool ("running", true);
			anima.SetBool("Jump",false);
				
		}

		if (gameObject.transform.position.y < -10f) {
			Application.LoadLevel("GameOver");		
		}

	}
	void OnCollisionEnter2D(Collision2D other)
	{
		airflag =true;											//tram dat
	}
	void OnTriggerEnter2D(Collider2D other){
		Debug.Log ("Trigger2D with"+other.gameObject.tag);
		if (other.gameObject.tag == "Coins") {
			Destroy(other.gameObject);	
			Instantiate(paraticle,other.gameObject.transform.position,other.gameObject.transform.localRotation);
		}

	}


}
